using UnityEngine;
using System.Collections;

public class ButtonGUI : MonoBehaviour {
	string split = "Split";
	string gen = "Generate Rooms";
	string corr = "Generate Corridors";	
	string all = "Generate All";
	string reset = "Reset All";
	string baseS = "Base size of dungeon:";
	string minR = "Minimum Split size:";
	string minS = "Minimum size of rooms:";
	string maxS = "Maximum size of rooms:";
	string numSpl = "Go";
	string splitNum = "Number of Splits to do";
	BSP_Rectangle canvas;
	public Texture Canvas;
	public Texture newTLine;
	BSP_Parameters param;
	BSP_Dungeon dungeon;
	

	// Use this for initialization
	void Start () {
		param = new BSP_Parameters();
		canvas = new BSP_Rectangle(150,0,Screen.width-150,Screen.height);
		//blank = new Texture2D(canvas.width,canvas.height,TextureFormat.ARGB32,false);
		//param = new BSP_Parameters();
		dungeon = new BSP_Dungeon();//param);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnGUI()
	{
		GUI.DrawTexture(new Rect(200,25,450,450),Canvas);
		if(GUI.Button(new Rect(10,50, 150,30), split))
		   {
			DoSplit();
			if(dungeon.vertical == false)
			GUI.DrawTexture(new Rect(dungeon.oldNodes[dungeon.geti()].splitLine.start.x,dungeon.oldNodes[dungeon.geti()].splitLine.start.y,(dungeon.oldNodes[dungeon.geti()].splitLine.end.x+dungeon.oldNodes[dungeon.geti()].splitLine.start.x),2),newTLine);
			if(dungeon.vertical == true)
			//GUI.DrawTexture(new Rect(dungeon.oldNodes[dungeon.geti()].splitLine.start.x,dungeon.oldNodes[dungeon.geti()].splitLine.start.y,2,(dungeon.oldNodes[dungeon.geti()].splitLine.end.y+dungeon.oldNodes[dungeon.geti()].splitLine.start.y)),newTLine);
		    GUI.DrawTexture(new Rect(dungeon.splitLine.start.x,dungeon.splitLine.start.y,dungeon.oldNodes[dungeon.geti()].getem('w'),10000),newTLine);
			//GUI.DrawTexture(new Rect(dungeon.splitLine.start.x,1,1,1),newTLine);
		}
		if(GUI.Button(new Rect(10,90, 150,30), gen))
		   {
			dungeon.generateRoom();
			}
		if(GUI.Button(new Rect(10,130,150,30), corr))
		   {
			dungeon.connectRooms();
			}
		if(GUI.Button(new Rect(10,180,150,30), all))
		{
			dungeon.generateRoom();
			dungeon.connectRooms();
		}
		if(GUI.Button(new Rect(10,230,150,30), reset))
		{
			resetAll();
		}
		if(GUI.Button(new Rect(10,270,150,30), numSpl))
		{
			switch (param.DoThisSplits)
			{
			case 1:
				DoSplit();
				dungeon.generateRoom();
				dungeon.connectRooms();
				break;
			case 2:
				DoSplit();
				DoSplit();
				dungeon.generateRoom();
				dungeon.connectRooms();
				break;
			case 3:
				DoSplit();
				DoSplit();
				DoSplit();
				dungeon.generateRoom();
				dungeon.connectRooms();
				break;
			case 4:
				DoSplit();
				DoSplit();
				DoSplit();
				DoSplit();
				dungeon.generateRoom();
				dungeon.connectRooms();
				break;
			default:
				DoSplit();
				break;
			}
		}
		string numSplit = (GUI.TextField(new Rect(10,320,150,30), splitNum));
		//int tempNum = int.Parse(numSplit);
		//param.DoThisSplits = tempNum;
		
		
		string temp = GUI.TextField(new Rect(10,360,100,30), baseS);
		//int temp1 = int.Parse(temp);
		//param.startHeight = temp1;
		//param.startWidth = param.startHeight;
		
		string temp2 = GUI.TextField(new Rect(10,400,100,30), minR);
		//int temp3 = int.Parse(temp2);
		//param.minSplit = temp3;
		
		string temp4 = GUI.TextField(new Rect(10,440,100,30), minS);
		//int temp5 = int.Parse(temp4);
		//param.minSplit = temp5;
		string temp6 = GUI.TextField(new Rect(10,470,100,30), maxS);
		//int temp7 = int.Parse(temp6);
		//param.maxSplit = temp7;
		
		
		
		
		
		
			
		
		  
	}
	
	void DoSplit()
	{
		dungeon = new BSP_Dungeon();
		if(dungeon != null)
		this.dungeon.BSP_Split(dungeon);
		else Debug.Log("Error");
		
	}
	
	void resetAll()
	{
		BSP_Parameters parameters = new BSP_Parameters();
		dungeon = new BSP_Dungeon(parameters);
		dungeon.root.addRectangle(new BSP_Rectangle(0,0,canvas.width,canvas.height));
	}
	void genRooms()
	{
		dungeon.displaySplits = false;
		dungeon.generateRoom();
	}	
}
